﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PMKB_DAL;
using PagedList;
using System.Linq.Expressions;

namespace PMKB_Repository
{
    public class clsLoadLinks
    {
        public string ConnectionName;
        public IPagedList< I_Link_OLAP> links;
        public static string[] tags(I_Link_OLAP o)
        {
            if (string.IsNullOrEmpty(o.Tags))
            {
                return null;
            }
            
            return o.Tags.Split(new string[1]{LinkAddOrEdit.separator},StringSplitOptions.RemoveEmptyEntries);
        }
        public string Search { get; set; }
        public void LoadLinks(int pageNumber, int PageSize, string OrderBy,bool Asc)
        {
            using (Link_OLAP_List ll = new Link_OLAP_List(ConnectionName))
            {

                if (string.IsNullOrEmpty(OrderBy))
                    OrderBy = Link_OLAP_List.FieldNames.LinkID;

                var iq = ll.LoadFromDB.LoadAllQ();
                var ordered=Link_OLAP_FindDB.OrderByField(iq,OrderBy);
                if(!string.IsNullOrEmpty(Search))
                {
                    ll.LoadFromDB.AddToCustomPredicate(Link_OLAP_FindDB.fexp_TagsContainsMultipleDef(Search));
                    ll.LoadFromDB.AddToCustomPredicate(Link_OLAP_FindDB.fexp_LinkUrlContainsMultipleDef(Search),false);
                    ll.LoadFromDB.AddToCustomPredicate(Link_OLAP_FindDB.fexp_LinkDescriptionContainsMultipleDef(Search), false);
                    ll.LoadFromDB.AddToCustomPredicate(Link_OLAP_FindDB.fexp_LinkNameContainsMultipleDef(Search), false);
                    ordered=ll.LoadFromDB.LoadFindCustomPredicateOrderedQ(OrderBy);
                }
                    
                links=ordered.ToPagedList(pageNumber,PageSize);
                
            }
        }
        public IPagedList<I_Link> LoadLinksSearch(int pageNumber, int PageSize, string OrderBy, bool Asc)
        {
            using (Link_List ll = new Link_List(ConnectionName))
            {

                if (string.IsNullOrEmpty(OrderBy))
                    OrderBy = Link_List.FieldNames.LinkID;

                var iq = ll.LoadFromDB.LoadAllQ();
                var ordered = Link_FindDB.OrderByField(iq, OrderBy);
                if (!string.IsNullOrEmpty(Search))
                {
                    ll.LoadFromDB.AddToCustomPredicate(Link_FindDB.fexp_LinkUrlContainsMultipleDef(Search), false);
                    ll.LoadFromDB.AddToCustomPredicate(Link_FindDB.fexp_LinkDescriptionContainsMultipleDef(Search), false);
                    ll.LoadFromDB.AddToCustomPredicate(Link_FindDB.fexp_LinkNameContainsMultipleDef(Search), false);
                    ordered = ll.LoadFromDB.LoadFindCustomPredicateOrderedQ(OrderBy);
                }

                return ordered.ToPagedList(pageNumber, PageSize);

            }
        }

    }
}
